package com.tdkj.service.common.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import com.tdkj.dao.common.IBeanTypeDao;
import com.tdkj.model.common.BeanType;
import com.tdkj.service.BaseService;
import com.tdkj.service.common.IBeanTypeService;
            
@Component
public class BeanTypeServiceImpl extends BaseService implements IBeanTypeService{
	
	private  IBeanTypeDao beanTypeDao;
	                              
	@Resource
	public void setBeanTypeDao(IBeanTypeDao beanTypeDao) {
		this.beanTypeDao = beanTypeDao;
	}

	@Override
	public BeanType createType(long parentId, String name) {
		BeanType parent = beanTypeDao.findById(parentId, BeanType.class);
		
		BeanType bean = new BeanType();
		bean.setName(name);
		bean.setParent(parent);
		bean.setLevel(parent.getLevel() + 1);
		bean.setRootName(parent.getRootName());
		beanTypeDao.save(bean);
		
		bean.setFullId(parent.getFullId() + "-" + bean.getId());
		
		return bean;
	}

	@Override
	public void deleteType(long id) {
		BeanType bean = beanTypeDao.findById(id, BeanType.class);
		beanTypeDao.delete(bean);
	}
	
	@Override
	public BeanType getRoot(String rootName) {
		BeanType bean = null;
		List<BeanType> beans =  beanTypeDao.findByProperty("parent", null, BeanType.class);
		if(beans != null && beans.size() != 0){
			for(BeanType type : beans){
				if(rootName.equals(type.getName())){
					bean = type;
					break;
				}
			}
		} 
		if(bean == null){
			bean = new BeanType();
			bean.setName(rootName);
			bean.setLevel(Constants.ROOT_LEVEL);
			bean.setRootName(rootName);
			beanTypeDao.save(bean);
			
			bean.setFullId(String.valueOf(bean.getId()));
		}
		return bean;
	}

	@Override
	public void updateType(long id, long parentId, String name) {
		BeanType parent = beanTypeDao.findById(parentId, BeanType.class);
		
		BeanType bean = beanTypeDao.findById(id, BeanType.class);
		bean.setName(name);
		bean.setParent(parent);
		bean.setLevel(parent.getLevel() + 1);
		bean.setRootName(parent.getRootName());
		bean.setFullId(parent.getFullId() + "-" + bean.getId());
	}

	@Override
	public BeanType getRootRecursive(String rootName) {
		BeanType bean = null;
		List<BeanType> beans =  beanTypeDao.findByProperty("parent", null, BeanType.class);
		if(beans != null && beans.size() != 0){
			for(BeanType type : beans){
				if(rootName.equals(type.getName())){
					bean = type;
					break;
				}
			}
		} 
		if(bean == null){
			bean = new BeanType();
			bean.setName(rootName);
			bean.setLevel(Constants.ROOT_LEVEL);
			bean.setRootName(rootName);
			beanTypeDao.save(bean);
			
			bean.setFullId(String.valueOf(bean.getId()));
		}
		Recursive(bean);
		
		return bean;
	}
	
	
	private void Recursive(BeanType parent){
		if(parent != null){
			for(BeanType child : parent.getChildren()){
				Recursive(child);
			}
		}
	}

	@Override
	public List<BeanType> getTypesByLevel(String rootName, Integer level) {
		BeanType example = new BeanType();
		example.setRootName(rootName);
		example.setLevel(level);
		
		return beanTypeDao.findByExample(example);
	}

}
